package com.car.commons.core.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    //自定义的秘钥
    //HS256：即字节长度>=32
    private static String userSect ="ksaasfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";

    //平台
    private static String adminSect ="ksaasfdewrwqrerwerwerewfdlkfjlasjflksajfd";

    //门店
    private static String clerkSect ="ksaasfdewrwqrerwerwerewfdlkfjlasjflksajfd";
    //生成token
    //键值对包含用户信息
    //以分钟为单位的时间 代表jwt的有效时间
    public static String createToken(Map<String, Object> body, Integer mil, String sect) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    //解析token
    //前置步骤是校验，校验会产生三种异常
    //格式异常，签名异常，超时异常
    public static Claims parseToken(String token, String sect) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }

    //生成管理员
    public static String createAdminToken(Map<String, Object> body, Integer mil) {
        return createToken(body, mil, adminSect);
    }

    //生成用户
    public static String createUserToken(Map<String, Object> body, Integer mil) {
        return createToken(body, mil, userSect);
    }

    //生成店员
    public static String createClerkToken(Map<String, Object> body, Integer mil) {
        return createToken(body, mil, clerkSect);
    }

    //解析管理员
    public static Claims parseAdminToken(String token) {
        Key key = Keys.hmacShaKeyFor(adminSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }

    //解析用户
    public static Claims parseUserToken(String token) {
        Key key = Keys.hmacShaKeyFor(userSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }

    //解析店员
    public static Claims parseClerkToken(String token) {
        Key key = Keys.hmacShaKeyFor(clerkSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }

    //获取用户用户ID
    public static Integer getUserId(String token) {
        Claims claims = parseToken(token, userSect);
        return Integer.parseInt(claims.get("userId").toString());
    }

    //获取用户管理员ID
    public static Integer getAdminId(String token) {
        Claims claims = parseToken(token, adminSect);
        return Integer.parseInt(claims.get("adminId").toString());
    }

    //获取店员ID
    public static Integer getClerkId(String token) {
        Claims claims = parseToken(token, clerkSect);
        return Integer.parseInt(claims.get("clerkId").toString());
    }

    //生成随机ID的main方法
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("adminId",1);
        System.out.println(createAdminToken(map,24*60));
    }


}
